import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
const key = "user";
const isLogin = "isLogin";
export default new Vuex.Store({
  state() {
    return {
      uid: null,
      isLogin: "0",
      token: "" //初始化token
    };
  },
  getters: {
    getStorage: function(state) {
      if (!state.user) {
        state.user = JSON.parse(localStorage.getItem(key));
        state.isLogin = localStorage.getItem(isLogin);
      }
      return state.user;
    }
  },
  mutations: {
    $_setLogin(state, value) {
      state.isLogin = value;
      localStorage.setItem(isLogin, value);
    },
    $_setStorage(state, value) {
      state.uid = value;
      localStorage.uid = value;
    },
    $_removeStorage(state) {
      state.uid = null;
      localStorage.removeItem(key);
    },
    //设置token等于外部传递进来的值
    setToken(state, token) {
      state.token = token;
      localStorage.token = token; //同步存储token至localStorage
    }
  },
  actions: {},
  modules: {}
});
